package com.sec.android.app.myfiles.listener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;

import com.sec.android.app.myfiles.listener.ListenerMgr.LifeCycle;
import com.sec.android.app.myfiles.util.UiUtils;

public class ConnectionChangeReceiver extends AbsBroadcastReceiverImp {

    public interface OnConnectionBroadcast {
        void onConnect();
        void onDisconnect();
    }

    OnConnectionBroadcast mProc;

    public ConnectionChangeReceiver(Context context, OnConnectionBroadcast proc, LifeCycle registerTime, LifeCycle unregisterTime) {
        super(context, registerTime, unregisterTime);
        mProc = proc;
    }

    @Override
    IntentFilter getIntentFilter() {
        IntentFilter ret = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        return ret;
    }

    @Override
    BroadcastReceiver getBroadcastReceiver() {
        if (mReceiver == null) {
            mReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (mProc != null) {
                        boolean networkOn = UiUtils.isNetworkOn(mContext);
                        if (networkOn)
                            mProc.onConnect();
                        else
                            mProc.onDisconnect();
                    }

                }
            };
        }
        return mReceiver;
    }

}
